#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStatusBar>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QLineEdit>

#include "searchdevice.h"
#include "serialportthread.h"
#include "data_package.h"

#include "equalizergroup.h"
#include "equalizerview.h"

#define CMD_INDEX_SYSTEM                0x01
#define SYSTEM_PARAM_INDEX_INFO         0x00

#define CMD_INDEX_EFFECT_REVERB         0x11
#define CMD_INDEX_EFFECT_REVERB_MIC     0x12
#define CMD_INDEX_EFFECT_DELAY          0x13
#define CMD_INDEX_EFFECT_EQ             0x14
#define CMD_INDEX_EFFECT_PRESET         0x15


#define EFFECT_PARAM_INDEX_REVERB_ENABLE    0
#define EFFECT_PARAM_INDEX_REVERB_DRY       1
#define EFFECT_PARAM_INDEX_REVERB_WET       2
#define EFFECT_PARAM_INDEX_REVERB_DAMP      3
#define EFFECT_PARAM_INDEX_REVERB_TONE      4
#define EFFECT_PARAM_INDEX_REVERB_FADEBACK  5


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    typedef struct
    {
        int16_t rev_enable;
        int16_t rev_dry;
        int16_t rev_wet;
        int16_t rev_damp;
        int16_t rev_tone;
        int16_t rev_fadeback;
        int16_t rev_mic_enable;
        int16_t rev_mic_dry;
        int16_t rev_mic_wet;
        int16_t rev_mic_damp;
        int16_t rev_mic_tone;
        int16_t rev_mic_fadeback;
        int16_t dly_enable;
        int16_t dly_time_ms;
        int16_t dly_mix;
        int16_t dly_tone;
        int16_t dly_fadeback;
        int16_t eq[16][4];
    }__command_effect_params;

    typedef struct
    {
        EqualizersGroup::FilterType type;
        float center_frequency;
        float q;
        float gain_db;
    }__filter_param;

    __command_effect_params cmd_effect_params;
    __filter_param preset_param_filter_pizeo[4][8];
    __filter_param preset_param_filter_mic[4][8];

    QStatusBar status_bar;
    QPushButton btn_bluetooth;
    QPushButton btn_searial;
    SearchDevice *search_device;
    QLabel lab_device_info;
    QWidget window;

    QLowEnergyCharacteristic bleReadChar;
    QLowEnergyCharacteristic bleWriteChar;
    QLowEnergyService *service;
    BTDevice *btDevice;

    SerialPortThread *serialPort;
    data_package package_proc;

    EqualizersGroup eqGroupMain;
    EqualizersGroup eqGroupMic;
    EqualizerView *eqViewMain;
    EqualizerView *eqViewMic;
    QGroupBox group_eq;
    QGroupBox group_eq_pizeo;
    QGroupBox group_eq_mic;

    QPushButton btn_export_data;

    QGridLayout layout_main;
    QGridLayout layout_eq;
    QGridLayout layout_eq_pizeo;
    QVBoxLayout layout_eq_pizeo_ctrl;
    QHBoxLayout layout_eq_pizeo_ctrl_cf;
    QHBoxLayout layout_eq_pizeo_ctrl_q;
    QHBoxLayout layout_eq_pizeo_ctrl_gain;
    QGridLayout layout_eq_mic;
    QVBoxLayout layout_eq_mic_ctrl;
    QHBoxLayout layout_eq_mic_ctrl_cf;
    QHBoxLayout layout_eq_mic_ctrl_q;
    QHBoxLayout layout_eq_mic_ctrl_gain;

    QLabel lab_eq_pizeo_cf;
    QLabel lab_eq_pizeo_q;
    QLabel lab_eq_pizeo_gain;
    QLineEdit txt_eq_pizeo_cf;
    QLineEdit txt_eq_pizeo_q;
    QLineEdit txt_eq_pizeo_gain;
    QLabel lab_eq_mic_cf;
    QLabel lab_eq_mic_q;
    QLabel lab_eq_mic_gain;
    QLineEdit txt_eq_mic_cf;
    QLineEdit txt_eq_mic_q;
    QLineEdit txt_eq_mic_gain;
    QPushButton btn_eq_clean_pizeo;
    QPushButton btn_eq_clean_mic;

    QPushButton btn_preset_1;
    QPushButton btn_preset_2;
    QPushButton btn_preset_3;
    QPushButton btn_preset_4;
    QPushButton btn_preset_save;
    QPushButton btn_preset_export;

    int eq_pizeo_current_select;
    int eq_mic_current_select;

    int preset_select_index;
    int max_preset;


    void send_command(uint8_t *command_data,size_t bytes);
    void update_device_info(uint8_t verf_hw,uint8_t verf_main,uint8_t verf_modify,QString name);
    void send_param_to_device_eq(int index);
    void command_get_preset(int index);


public slots:
    void on_btn_click_bluetooth(void);
    void on_btn_click_searial(void);
    void on_device_is_connect(void);
    void on_communication_connection(bool status);
    void on_communication_disconnect(void);
    void on_command_analyze(QByteArray data);
    void on_eq_pizeo_select_change(int index);
    void on_eq_pizeo_select_release(int index);
    void on_eq_mic_select_change(int index);
    void on_eq_mic_select_release(int index);
    void on_btn_click_eq_clean_pizeo(void);
    void on_btn_click_eq_clean_mic(void);
    void slot_le_textedited(void);
    void on_btn_click_preset(void);
    void on_btn_click_preset_save(void);
    void on_btn_click_preset_export(void);

};
#endif // MAINWINDOW_H

